var test = require('tape');

function match(target, source) {
    let key;
    for (key in source) {
        if (~~source[key] != ~~target[key]) {
            return false;
        }
    }
    return true;
}

let timestamp = 0
function mockEvent(...xy) {
    const touches = [];
    for (let i = 0; i < xy.length; i += 2) {
        touches[i / 2] = {
            pageX: xy[i],
            pageY: xy[i + 1],
        };
    }
    return {
        timeStamp: timestamp+=10,
        touches,
        preventDefault() { },
        stopPropagation() { }
    };
}

const engine = pz._r;
// engine.options.ending = false;  // disable ending animation

window.setTimeout(function () {
    /*
    test('test touch', function (t) {
        const elPosDecribe = getElTransform();
        let resultTransform, expectTransform;

        // Test position update
        Object.assign(engine.elProperties, { x: 100, y: 100, scale: 1 });
        engine.updateElPos();
        resultTransform = getElTransform();
        expectTransform = { x: 100, y: 100, scale: 1 };

        if (match(resultTransform, expectTransform)) {
            t.pass('no scale match pass.');
        } else {
            console.log(resultTransform);
            t.fail('no scale match fail.');
        }


        // Test touch move
        engine.event.start(mockEvent(0, 0));
        engine.event.move(mockEvent(80, 80));
        engine.event.end(mockEvent(80, 80));
        resultTransform = getElTransform();
        expectTransform = { x: 180, y: 180, scale: 1 };

        if (match(resultTransform, expectTransform)) {
            t.pass('no scale move pass.');
        } else {
            console.log('RESULT', resultTransform);
            console.log('EXPECT', expectTransform);
            t.fail('no scale move fail.');
        }

        t.end();
    });
    */

    const rectWrap = engine.wrapper.getBoundingClientRect();
    const rectCtrlNoScale = engine.el.getBoundingClientRect();

    test('Pinch', function (t) {
        // Prepare zoom.
        const cetnerOfEventPage = {
            x: rectWrap.left + rectWrap.width / 2,
            y: rectWrap.top + rectWrap.height / 2
        }
        // Locate scale center in view.
        const centerTranslate = {
            x: rectCtrlNoScale.width / -2 + rectWrap.width / 2 + rectWrap.left,
            y: rectCtrlNoScale.height / -2 + rectWrap.height / 2 + rectWrap.top,
            scale: 1
        };
        // translate(-715px, 22.125px) scale(1)
        Object.assign(engine.elProperties, centerTranslate);
        engine.updateElPos();
        
        // Test 2x zoom horizontal
        engine.event.start(mockEvent(0, 0));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x - 50, cetnerOfEventPage.y,
            cetnerOfEventPage.x + 50, cetnerOfEventPage.y
        ));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x - 100, cetnerOfEventPage.y,
            cetnerOfEventPage.x + 100, cetnerOfEventPage.y
        ));
        engine.event.end(mockEvent(0, 0));
        // translate(-715px, 22.125px) scale(2)
        resultTransform = getElTransform();
        expectTransform = {
            x: centerTranslate.x,
            y: centerTranslate.y,
            scale: 2
        };
        // Test engine.scaleOriginDiff
        // t.equal(`${engine.scaleOriginDiff.x}, ${engine.scaleOriginDiff.y}`, '-175, 0', 'test engine scaleOriginDiff');
        if (match(resultTransform, expectTransform)) {
            t.pass('2x zoom horizontal pass.');
        } else {
            console.log('RESULT', resultTransform);
            console.log('EXPECT', expectTransform);
            t.fail('2x zoom horizontal fail.');
        }



        // Locate scale center in view.
        Object.assign(engine.elProperties, {
            x: rectCtrlNoScale.width / -2 + rectWrap.width / 2 + rectWrap.left,
            y: rectCtrlNoScale.height / -2 + rectWrap.height / 2 + rectWrap.top,
            scale: 1
        });
        engine.updateElPos();

        // Test 2x zoom vertical
        engine.event.start(mockEvent(0, 0));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x, cetnerOfEventPage.y - 50,
            cetnerOfEventPage.x, cetnerOfEventPage.y + 50
        ));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x, cetnerOfEventPage.y - 100,
            cetnerOfEventPage.x, cetnerOfEventPage.y + 100
        ));
        engine.event.end(mockEvent(0, 0));
        
        // translate(-715px, 22.125px) scale(2)
        resultTransform = getElTransform();
        expectTransform = {
            x: centerTranslate.x,
            y: centerTranslate.y,
            scale: 2
        };
        // Test engine.scaleOriginDiff
        // t.equal(`${engine.scaleOriginDiff.x}, ${engine.scaleOriginDiff.y}`, '-175, 0', 'test engine scaleOriginDiff');
        if (match(resultTransform, expectTransform)) {
            t.pass('2x zoom vertical pass.');
        } else {
            console.log('RESULT', resultTransform);
            console.log('EXPECT', expectTransform);
            t.fail('2x zoom vertical fail.');
        }


        // Pinch in (10px, 10px) 2x
        engine.event.start(mockEvent(0, 0));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x, cetnerOfEventPage.y - 10,
            cetnerOfEventPage.x + 20, cetnerOfEventPage.y - 10
        ));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x - 10, cetnerOfEventPage.y - 10,
            cetnerOfEventPage.x + 30, cetnerOfEventPage.y - 10
        ));
        // translate(-725px, 32.125px) scale(4)
        resultTransform = getElTransform();
        expectTransform = {
            x: centerTranslate.x - 10,
            y: centerTranslate.y + 10,
            scale: 4
        };

        if (match(resultTransform, expectTransform)) {
            t.pass('4x zoom offset (10px, 10px) pass.');
        } else {
            console.log('RESULT', resultTransform);
            console.log('EXPECT', expectTransform);
            t.pass('4x zoom offset (10px, 10px) fail.');
        }


        engine.event.move(mockEvent(
            cetnerOfEventPage.x - 15, cetnerOfEventPage.y - 10,
            cetnerOfEventPage.x + 35, cetnerOfEventPage.y - 10
        ));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x - 18, cetnerOfEventPage.y - 10,
            cetnerOfEventPage.x + 38, cetnerOfEventPage.y - 10
        ));
        engine.event.move(mockEvent(
            cetnerOfEventPage.x - 20, cetnerOfEventPage.y - 10,
            cetnerOfEventPage.x + 40, cetnerOfEventPage.y - 10
        ));
        // width: 1750px; transform: translate(-725px, 32.125px) scale(2);

        engine.event.end(mockEvent(0, 0));

        t.end();
    });
}, 100);

